import { region } from './region'

export function getRegion(id) {
  id = String(id)
  let tmp = id.split('')
  tmp.splice(2, 4, 0, 0, 0, 0)
  const provinceId = tmp.join('')
  const index = region.findIndex(item => item.value === provinceId)
  let province = null
  province = region[index]
  if (id === provinceId) { // 如果只是第一级的id
    console.log('first: ', province)
    return [id]
  } else if (!province.children[0].children) { // 只是第二级的id
    console.log('not three: ', province)
    return [provinceId, id]
  } else { // 如果有第三级地区时的情况
    const secondIds = province.children.map(item => item.value)

    if (secondIds.includes(id)) { // 是第二级的id
      // const index = secondIds.indexOf(id)
      return [provinceId, id]
    } else { // 是第三级的id
      let secondId = id.split('')
      secondId.splice(4, 2, 0, 0)
      secondId = secondId.join('')
      // const index = secondIds.indexOf(secondId)
      return [provinceId, secondId, id]
    }
  }
}
